#pragma once
#include <iostream>
#include<jsoncpp/json/json.h>
using std::string;
const std::string sep = " ";
const string pro_sep = "\n";

#define Myself 1
std::string Encode(const std::string &content)
{
    // 封装报头
    std::string package = std::to_string(content.size());
    package += pro_sep;
    package += content;
    package += pro_sep;


    return package;
}
// 5'\n'1 + 1'\n'
bool Decode(std::string &package, std::string *content)
{
    std::size_t pos = package.find(pro_sep);
    if(pos == std::string::npos) return false;
    std::string len_str = package.substr(0, pos);
    std::size_t len = std::atoi(len_str.c_str());
    // package = len_str + content_str + 2
    std::size_t total_len = len_str.size() + len + 2;
    if(package.size() < total_len) return false;
    *content = package.substr(pos + 1, len);
    // earse 移除报文 package.erase(0, total_len);
     package.erase(0, total_len);

    return true;
}
class Request
{
public:
    Request(int _x, int _y, char _op)
        : x(_x), y(_y), op(_op)
    {}
    Request()
    {}
    int x, y;
    char op; // + - * / %
    bool Serialize(std::string *out)
    {
#ifdef Myself
        // 构建报文的有效载荷
        // struct => string, x op y "len"\n"x op y"
        string s = std::to_string(x);
        s += sep;
        s += op;
        s += sep;
        s += std::to_string(y);

        *out = s;
        // protocol : //5 '\n' "100 + 200" \n
#else 
        Json::Value root;
        root["x"] = x;
        root["y"] = y;
        root["op"] = op;
        Json::FastWriter w;
       *out = w.write(root);
        return true;
#endif
        //return true;
    }
    bool Reserialize(const std::string &in)
    {
#ifdef Myself
        //"1 + 1"
        // sscanf(in.c_str(),"%d %c %d",x,op,y);
        auto l = in.find(sep);

        if(l == string::npos)  return false;
         x = std::atoi(in.substr(0,l).c_str());

        auto r = in.rfind(sep);
        if(r == string::npos)  return false;

        y = std::atoi(in.substr(r + 1).c_str());
        if(l + 2 != r) return false;
        op = in[l + 1];
        return true;
#else
        Json::Value v;
        Json::Reader r;
        r.parse(in,v);
        x = v["x"].asInt();
        y = v["y"].asInt();
        op = v["char"].asInt();
        return true;
#endif
    }

    void CreateTask()
    {
        cout << "构建任务:" << x << ' ' << op << ' ' << y << "=?" << endl;
    }
};

class Response
{
public:
    Response(int _r = 0, int _c = 0) : res(_r), code(_c)
    {}
    int res;
    int code; // 0 可信,否则!0具体是几就表明对应的错误原因
    bool Serialize(std::string *out)
    {
#ifdef Myself
        //"len"\n"result code"
        string s = std::to_string(res);
        s += sep;
        s += std::to_string(code);
        *out = s;
        return true;        
#else
    Json::Value v;
    v["res"] = res;
    v["code"] = code;
    Json::FastWriter w;
    *out = w.write(v);
    return true;
#endif
    
    }
    bool Reserialize(const std::string &in)
    {
#ifdef Myself
        // "1000 0"
        auto l = in.find(sep);
        if(l == string::npos)  return false;
        res = std::atoi(in.substr(0,l).c_str());
        auto r = in.rfind(sep);
        if(r == string::npos)  return false;
      
        code = std::atoi(in.substr(r + 1).c_str());
        return true;
#else
        Json::Value v;
        Json::Reader r;
        r.parse(in,v);
        res = v["res"].asInt();
        code = v["code"].asInt();
        return true;
#endif
    }
};